
function readIndividus(xml){
	var p = new Array();
	elements = xml.childNodes;
	
	for(element in elements){
		parent = new Individu();
		for(child in element.childNodes){
			if(child.nodeName == "property"){
				switch(child.getElementsByTagName('name')[0].nodeValue){
					case "id":
								parent.id = parseInt(child.getElementsByTagName('value')[0].nodeValue);
								break;
					case "RESOLUTION_idResolution":
								parent.idResolution = parseInt(child.getElementsByTagName('value')[0].nodeValue);
								break;
					case "idParent1":
								parent.idParent1 = parseInt(child.getElementsByTagName('value')[0].nodeValue);
								break;
					case "idParent2":
								parent.idParent2 = parseInt(child.getElementsByTagName('value')[0].nodeValue);
								break;
					default: break;
				}
			}
			if(child.nodeName == "foreigners"){
				for(foreign in child.childNodes){
					type = foreign.getAttribute("type");
					tagId = "";
					values = new Array();
					if(type == "allele"){
						for(property in foreign.childNodes){
							name = property.getElementsByTagName('name')[0].nodeValue;
							if(name == "ALLELE_idALLELE"){
								values['id'] = parseInt(property.getElementsByTagName('value')[0].nodeValue);
							}
							if(name == "valeur"){
								values['valeur'] = parseFloat(property.getElementsByTagName('value')[0].nodeValue);
							}
							if(name == "valeurBinaire"){
								values['valeurBinaire'] = property.getElementsByTagName('value')[0].nodeValue;
							}
						}
						obj = parent.NAMES['chromosome'][values['id']];
						this.chromosome['alleles'][obj] = values['valeurBinaire'];
						this.chromosome['valeurs'][obj] = values['valeur'];
					}
					if(type == "variable"){
						for(property in foreign.childNodes){
							name = property.getElementsByTagName('name')[0].nodeValue;
							if(name == "VARIABLE_idVARIABLE"){
								values['id'] = parseInt(property.getElementsByTagName('value')[0].nodeValue);
							}
							if(name == "valeurVariable"){
								values['valeurVariable'] = parseFloat(property.getElementsByTagName('value')[0].nodeValue);
							}
						}
						obj = parent.NAMES['variables'][values['id']];
						this.variables[obj] = values['valeurVariable'];
					}
					if(type == "contrainte"){
						for(property in foreign.childNodes){
							name = property.getElementsByTagName('name')[0].nodeValue;
							if(name == "CONTRAINTE_idCONTRAINTE"){
								values['id'] = parseInt(property.getElementsByTagName('value')[0].nodeValue);
							}
							if(name == "satisfaite"){
								values['satisfaite'] = parseFloat(property.getElementsByTagName('value')[0].nodeValue);
							}
						}
						obj = parent.NAMES['contraintes'][values['id']];
						this.contraintes[obj] = values['satisfaite'];
					}
					if(type == "critere"){
						for(property in foreign.childNodes){
							name = property.getElementsByTagName('name')[0].nodeValue;
							if(name == "CRITERE_idCRITERE"){
								values['id'] = parseInt(property.getElementsByTagName('value')[0].nodeValue);
							}
							if(name == "fitness"){
								values['fitness'] = parseFloat(property.getElementsByTagName('value')[0].nodeValue);
							}
						}
						obj = parent.NAMES['criteres'][values['id']];
						this.criteres[obj] = values['fitness'];
					}
				}
			}
		}
		p.push(parent);
	}
	
	postMessage('passe');
	return p;
}

function makeIndividus(listeParents){
	var liste = new Array();
	for(i = 0 ; i < listeParents.length;i++){
		liste.push(new Individu());
	}
	return liste;
}

function writeIndividu(indiv){
	doc = new DOMDocument();
	
	root = doc.createElement('message');
	element = doc.createElement('element');
	foreigners = doc.createElement('foreigners');
	
	property = doc.createElement('property');
	name = doc.createElement('name');
	value = doc.createElement('value');
	
	name.nodeValue = "idresolution";
	value.nodeValue = indiv.idResolution;
	
	property.appendChild(name);
	property.appendChild(value);
	element.appendChild(property);
		
	property = doc.createElement('property');
	name = doc.createElement('name');
	value = doc.createElement('value');
	
	name.nodeValue = "idparent1";
	value.nodeValue = indiv.idParent1;
		
	property = doc.createElement('property');
	name = doc.createElement('name');
	value = doc.createElement('value');
	
	name.nodeValue = "idparent2";
	value.nodeValue = indiv.idParent2;
	
	property.appendChild(name);
	property.appendChild(value);
	element.appendChild(property);
	
	property.appendChild(name);
	property.appendChild(value);
	element.appendChild(property);
	
	for(i = 0; i< indiv.chromosome['alleles'].length ; i++){
		//indiv.chromosome['alleles']
		foreign = doc.createElement('foreign');
		foreign.setAttribute('type','allele')
		property = doc.createElement('property');
		name = doc.createElement('name');
		value = doc.createElement('value');
		
		name.nodeValue = "id";
		value.nodeValue = indiv.ID['chromosome'][i];
		
		property.appendChild(name);
		property.appendChild(value);
		foreign.appendChild(property);
		
		property = doc.createElement('property');
		name = doc.createElement('name');
		value = doc.createElement('value');
		
		name.nodeValue = "valeur";
		value.nodeValue = indiv.chromosome['valeurs'][i];
		
		property.appendChild(name);
		property.appendChild(value);
		foreign.appendChild(property);
		
		property = doc.createElement('property');
		name = doc.createElement('name');
		value = doc.createElement('value');
		
		name.nodeValue = "valeurBinaire";
		value.nodeValue = indiv.chromosome['alleles'][i];
		
		property.appendChild(name);
		property.appendChild(value);
		foreign.appendChild(property);
		
		foreigners.appendChild(foreign);
	}
	
	for(variable in indiv.NAMES['variables']){
		//indiv.chromosome['alleles']
		foreign = doc.createElement('foreign');
		foreign.setAttribute('type','variable')
		property = doc.createElement('property');
		name = doc.createElement('name');
		value = doc.createElement('value');
		
		name.nodeValue = "id";
		value.nodeValue = indiv.ID['variables'][variable];
		
		property.appendChild(name);
		property.appendChild(value);
		foreign.appendChild(property);
		
		property = doc.createElement('property');
		name = doc.createElement('name');
		value = doc.createElement('value');
		
		name.nodeValue = "valeurVariable";
		value.nodeValue = indiv.variables[variable];
		
		property.appendChild(name);
		property.appendChild(value);
		foreign.appendChild(property);
		
		foreigners.appendChild(foreign);
	}
		
	for(contrainte in indiv.NAMES['contraintes']){
		//indiv.chromosome['alleles']
		foreign = doc.createElement('foreign');
		foreign.setAttribute('type','contrainte')
		property = doc.createElement('property');
		name = doc.createElement('name');
		value = doc.createElement('value');
		
		name.nodeValue = "id";
		value.nodeValue = indiv.ID['contraintes'][contrainte];
		
		property.appendChild(name);
		property.appendChild(value);
		foreign.appendChild(property);
		
		property = doc.createElement('property');
		name = doc.createElement('name');
		value = doc.createElement('value');
		
		name.nodeValue = "satisfaite";
		value.nodeValue = indiv.contraintes[contrainte];
		
		property.appendChild(name);
		property.appendChild(value);
		foreign.appendChild(property);
		
		foreigners.appendChild(foreign);
	}
	
	for(critere in indiv.NAMES['criteres']){
		//indiv.chromosome['alleles']
		foreign = doc.createElement('foreign');
		foreign.setAttribute('type','critere')
		property = doc.createElement('property');
		name = doc.createElement('name');
		value = doc.createElement('value');
		
		name.nodeValue = "id";
		value.nodeValue = indiv.ID['criteres'][critere];
		
		property.appendChild(name);
		property.appendChild(value);
		foreign.appendChild(property);
		
		property = doc.createElement('property');
		name = doc.createElement('name');
		value = doc.createElement('value');
		
		name.nodeValue = "fitness";
		value.nodeValue = indiv.criteres[critere];
		
		property.appendChild(name);
		property.appendChild(value);
		foreign.appendChild(property);
		
		foreigners.appendChild(foreign);
	}
	
	element.appendChild(foreigners);
	root.appendChild(element);
	doc.appendChild(root);
	
	postMessage("coucou : <br />"+getXML(doc));
	
	send("../../display.php","request="+getXML(doc)); 
}

function getXML(aNode) {
  var out = '';
  if(aNode.nodeType == Node.ELEMENT_NODE) {
    out = '<'+aNode.nodeName;
    if(aNode.hasAttributes()) {
      var atts = aNode.attributes;
      for(var x=0;x<atts.length;x++) {
        out +=' '+atts[x].nodeName+'="'+atts[x].nodeValue+'"';
      }
    }
    
    if(aNode.hasChildNodes()) {
      out += '>';
      var kids = aNode.childNodes;
      for(var x=0;x<kids.length;x++) {
        switch(kids[x].nodeType) {
          case Node.ELEMENT_NODE:
            out += getXML(kids[x]);
            break;
          case Node.TEXT_NODE:
            out += kids[x].nodeValue;
            break;
          case Node.COMMENT_NODE:
            out += '<!--'+kids[x].nodeValue+'-->';
            break;
          case Node.CDATA_SECTION_NODE:
            out += '<'+'![CDATA['+kids[x].nodeValue+']'+']>';
            break;  
        }
      }
      out += '</'+aNode.nodeName+'>';
    } else { 
      out += '/>';
    }
  }  
  return out;
}
